<?php
namespace ProjetA\GroupeBundle\Form;

use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManager;

use ProjetA\GroupeBundle\Entity\Page as Page;
use ProjetA\GroupeBundle\Entity\Attribut;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of PageHandler
 *
 * @author Gaël Francillette  francillette.gael@etu.unice.fr
 */
class PageHandler {
    protected $form;
    protected $request;
    protected $em;
    
    public function __construct(Form $form, Request $request, EntityManager $em)
    {
        $this->form    = $form;
        $this->request = $request;
        $this->em      = $em;
    }

    public function process()
    {
        if( $this->request->getMethod() == 'POST' )
        {
            $this->form->bindRequest($this->request);

            if( $this->form->isValid() )
            {
                $this->onSuccess($this->form->getData());

                return true;
            }
        }

        return false;
    }
    /**
     *[projeta_groupebundle_groupetype] => Array
                        (
                            [_token] => 958b7c5bcde651ff78dd57772ca5a2ca92e161a3
                            [nom] => hgg
                            [description] => ggg
                            [page_id] => 2
                            [public] => 0
                            [page] => Array
                                (
                                    [Nom] => Array
                                        (
                                            [name] => Nom
                                            [text] => gg
                                        )

                                    [Prenom] => Array
                                        (
                                            [name] => Prenom
                                            [text] => gg
                                        )

                                    [email] => Array
                                        (
                                            [name] => email
                                            [text] => gg
                                        )

                                )

                        )
     * @param array $dataform 
     */

    public function onSuccess( Page $page)
    {
        $this->em->persist($page);
        foreach($page->getAttributs() as $attribut)  
            {
                $this->em->persist($attribut);
            }
        $this->em->flush();
    }
}

?>
